#!/bin/sh
path_movie="/downloads/s/P2P.m4v"
path_torrent="/downloads/s/P2P.torrent"

#number of downloading peers
max=3 
# strategy we want to use, 1=SlidingWindowAlgoritme, 2=SegmentsAlgoritme, any other numbers is RarestFirstAlgoritme
STRATEGY=1

minport="6881"
maxport="6889"

#Please change this to reflect your own path to the source/jar file
project_home="/Users/Siyalrach/Workspace/jbittorrentapi"

cd ${project_home}

export CLASSPATH=${project_home}/build:${project_home}/ext/simple-upload-0.3.4.jar:${project_home}/ext/ant.jar:${project_home}/ext/freemarker.jar:${project_home}/ext/groovy.jar:${project_home}/ext/jaxen-core.jar:${project_home}/ext/jaxen-jdom.jar:${project_home}/ext/jdom.jar:${project_home}/ext/kxml.jar:${project_home}/ext/saxpath.jar:${project_home}/ext/simple-upload-0.3.4.jar:${project_home}/ext/velocity.jar:${project_home}/ext/velocity.jar:${project_home}/ext/xalan.jar:${project_home}/ext/xerces.jar:${project_home}/ext/xml-apis.jar

#clear tracker
cd ${project_home}/example/tracker
ls |grep -v s|xargs rm -Rf

#clear downloads folder, except the folder s
cd ${project_home}/downloads
ls |grep -v s|xargs rm -rf

cd ${project_home}


#start tracker
xterm -e "java -jar P2PP_DELTA_TRACKER.jar example_tracker_config.xml" &

sleep "2"

#start super peer
java -jar P2PP_DELTA_SUPERPEER.jar ${project_home}${path_torrent} http://localhost:8081/announce 256 ${project_home}${path_movie} nick bla http://localhost:8081/upload none none bla $minport $maxport &

sleep "5"

#download torrent
i=1
while [ $i -le $max ]
do
	RANDOMUP=$((RANDOM%10+5))
	RANDOMDW=$((RANDOM%5+0))

	java -Xmx512M -jar P2PP_DELTA_PEER.jar $i ${project_home}${path_torrent} $minport $maxport $RANDOMUP $RANDOMDW $STRATEGY &
  
	i=$(($i+1))

	sleep "2"
done
